
var SERVER_PORT = "http://120.77.223.88:8080/red/";

var index = 0;
var timer;
var container = document.getElementById("banner");

var index = new Vue({
    el: "#app-index",
    data: {
        //登录账户
        isLogin:false,
        phone: "",
        nickname:"",

        //
        order:[],

        //目录树
        categoryTree: [],

        //根据一级目录排布的商品
        goodsWithPrimCateID: [],


        //当前用于展示的一级分类
        currentGoodsInPrimToDisplay: [],

        //当前二级目录
        currentSubCategory: [],

        //当前一级目录
        currentPrimCategory: [],

        searchContent:"",

        //post
        imgMini_post:[
            "images/b1.jpg",
            "images/b2.jpg",
            "images/b3.jpg",
            "images/b4.jpg",
            "images/b5.jpg",
            "images/b6.jpg",
            "images/b7.jpg",
            "images/b8.jpg",
        ],

        image_post:[
            "images/1.jpg",
            "images/2.jpg",
            "images/3.jpg",
            "images/4.jpg",
            "images/5.jpg",
            "images/6.jpg",
            "images/7.jpg",
            "images/8.jpg",
        ]
    },
    methods: {
        //退出登录
        quitLogin: function () {
            this.isLogin = false;
            this.phone = "";
            this.nickname = "";
            window.localStorage.setItem("isLogin", false);
            window.location.reload(true);
        },


        //得到所有一级目录
        getAllCategory: function () {
            this.$http.get(SERVER_PORT + "requestCategory").then(
                response => {
                    //console.log(response.data.data);
                    var originalChaoticInfo = response.data.data;

                    for (var index in originalChaoticInfo) {
                        var primaryCategoryName = originalChaoticInfo[index].category1_Name;
                        var primaryCategoryID = originalChaoticInfo[index].category1_ID;

                        var primaryCategory = {
                            name: primaryCategoryName,
                            ID: primaryCategoryID
                        }

                        var secondCategory = [];

                        for (var sub_index in originalChaoticInfo[index].category2) {
                            var secondCategoryName =
                                originalChaoticInfo[index].category2[sub_index].name;
                            var category2_ID =
                                originalChaoticInfo[index].category2[sub_index].category2_ID;
                            secondCategory.push({
                                name: secondCategoryName,
                                ID: category2_ID
                            });
                        }

                        this.categoryTree.push({
                            primaryCategory: primaryCategory,
                            secondCategory: secondCategory
                        });
                    }

                    this.currentPrimCategory = this.categoryTree[0].primaryCategory;

                    //根据一级目录得到所有的商品
                    for (var index in this.categoryTree) {
                        this.requestGoodsByPrimCateId(this.categoryTree[index].primaryCategory.ID);
                    }



                    //this.sellers = Object.assign([], response.data.data);
                    // 响应成功回调
                },
                response => {
                    // 响应错误回调
                    alert("请求失败");
                }
            );
        },

        //根据一级目录ID请求商品
        requestGoodsByPrimCateId: function (ID) {
            //console.log(ID);
            this.$http.post(SERVER_PORT + "requestGoodsInCategory1", { category1_ID: ID }, {
                emulateJSON: true
            }).then((response) => {
                this.goodsWithPrimCateID.push({
                    ID: ID,
                    allGoods: response.data.data
                });
                if (this.goodsWithPrimCateID.length == this.categoryTree.length) {
                    //alert("加载完成");
                    this.currentGoodsInPrimToDisplay = this.goodsWithPrimCateID[0];
                    this.currentSubCategory = this.categoryTree[0].secondCategory;
                    this.marryOrder();
                }
            }, (error) => {
                alert("请求失败");
            })
        },

        onPrimCateClick: function (index) {
            //console.log(index);
            this.currentGoodsInPrimToDisplay = this.goodsWithPrimCateID[this.order[index]];
            this.currentPrimCategory = this.categoryTree[index].primaryCategory;
            this.currentSubCategory = this.categoryTree[index].secondCategory;
        },

        onSubCateClick: function (index){
            console.log(index);

            var subID = this.currentSubCategory[index].ID;
            window.localStorage.setItem("subID",subID);
            //window.localStorage.setItem("subID",subID);
            window.location.href="searchResult.html";
        },

        onGoodItemClick:function(index){
            var ID = this.currentGoodsInPrimToDisplay.allGoods[index].goods_ID;
            //window.localStorage.setItem("good_id",ID);
            window.localStorage.setItem("good_id",ID);
            window.location.href="pro-detail.html"
        },

        onPostItemClick:function(){


            var index = 0;
            var timer;
            var container = document.getElementById("banner");
    
            function play() {
                timer = setInterval(function () {
                    $(".btn-right").click();
                }, 1500);
            }
    
            function stop() {
                clearInterval(timer);
            }
    
            $(".btn-item").click(function () {
                //console.log(index)
                index = $(this).index();//获取点击li的下标
                $(this).addClass("active").siblings().removeClass("active");
                $(".img-item").eq(index).fadeIn().siblings().fadeOut();
            });
    
            $(".btn-right").click(function () {
                index++;
                if (index > 7) index = 0;
    
                $(".img-item").eq(index).fadeIn().siblings().fadeOut();
                $(".btn-item").eq(index).addClass("active").siblings().removeClass("active");
            });
    
            $(".btn-left").click(function () {
                index--;
                if (index < 0) {
                    index = 8;
                }
                $(".img-item").eq(index).fadeIn().siblings().fadeOut();
                $(".btn-item").eq(index).addClass("active").siblings().removeClass("active");
            });
    
            container.onmouseover = stop;
            container.onmouseout = play;
        },

        //匹配导航栏与商品问题
        marryOrder:function(){
            for (var i in this.categoryTree) {
                for(var j in this.goodsWithPrimCateID)
                if (this.categoryTree[i].primaryCategory.ID == this.goodsWithPrimCateID[j].ID) {
                    this.order.push(j)
                }
            }
        },

        search:function(){

            if(this.searchContent.length==0)
            {
                Ply.dialog("alert", "输入不能为空").done(function () {
                });
                return;
            }
            window.localStorage.setItem("searchContent",this.searchContent);
            //window.localStorage.setItem("searchContent",this.searchContent);
            window.location.href="searchResult.html";
        }

    },

    created() {
        if (window.localStorage.getItem("isLogin") !== undefined && window.localStorage.getItem("isLogin") != "false") {
            this.isLogin = true;
            this.nickname = window.localStorage.getItem("nickname");
            this.phone = window.localStorage.getItem('phone');
        }
        this.getAllCategory();
    }
});